' Strict
SuperStrict

' Modules
Import BRL.Max2D
'Import BtbN.GLDraw2D

' Files
Import "../Global.bmx"
Import "../TLog.bmx"
Import "../TINILoader.bmx"
Import "../TInputSystem.bmx"
Import "../Utilities/Log.bmx"
Import "../Utilities/Math.bmx"
Import "../Utilities/Graphics.bmx"

' Widgets
Include "TSkin.bmx"
Include "TWidget.bmx"
Include "TContainer.bmx"
Include "TWindow.bmx"
Include "TLabel.bmx"
Include "TImageBox.bmx"
Include "TCheckBox.bmx"
Include "TSlider.bmx"
Include "TButton.bmx"
Include "TGroup.bmx"
Include "TPopupMenu.bmx"
Include "TTextField.bmx"

' TGUI
Type TGUI
	Global initializedClass:Int = 0
	Global logger:TLog
	
	Field root:TWidget
	Field popupMenuContainer:TWidget
	Field alpha:Float
	Field cursor:TCursor
	Field skin:TSkin
	
	Field eraseEvents:Int
	Field eraseMouseEvents:Int
	Field oldEraseMouseEvents:Int
	Field focusWidget:TWidget
	Field newFocusWidget:TWidget
	
	Field noFocusAlpha:Float
	Field shadowOffsetX:Int
	Field shadowOffsetY:Int
	Field shadowIntensity:Float
	
	' Init
	Method Init()
		If initializedClass = False
			TGUI.InitClass()
		EndIf
		
		TGUI.logger.Write("Initializing GUI")
		
		Self.root = TContainer.Create("root")
		Self.root.gui = Self
		
		Self.popupMenuContainer = TContainer.Create("_popupMenuContainer")
		Self.popupMenuContainer.gui = Self
		
		Self.skin = TSkin.Create("Simple")
		
		Self.alpha = 1.0
		Self.shadowOffsetX = 3
		Self.shadowOffsetY = 3
		Self.shadowIntensity = 0.25
		Self.noFocusAlpha = 0.75
		Self.eraseEvents = False
		Self.eraseMouseEvents = False
		Self.focusWidget = Null
		Self.newFocusWidget = Self.root
	End Method
	
	' Add
	Method Add(widget:TWidget)
		Self.root.Add(widget)
	End Method
	
	' AddMenu
	Method AddMenu(widget:TWidget)
		Self.popupMenuContainer.Add(widget)
	End Method
	
	' Update
	Method Update()
		' Update menu first
		Self.popupMenuContainer.UpdateAll() 
		
		' Erase mouve events until mouse is not pressed anymore
		If Self.eraseMouseEvents
			If Self.oldEraseMouseEvents = Self.eraseMouseEvents And TInputSystem.GetMouseDown(1) = 0 And TInputSystem.GetMouseDown(2) = 0 And TInputSystem.GetMouseDown(3) = 0
				Self.eraseMouseEvents = False
			EndIf
			
			TInputSystem.EraseMouseEvents()
		EndIf
		Self.oldEraseMouseEvents = Self.eraseMouseEvents
		
		' Update all widgets
		Self.root.UpdateAll()
		
		If Self.eraseEvents
			TInputSystem.EraseAllEvents()
			Self.eraseEvents = False
		EndIf
		
		If Self.focusWidget <> Self.newFocusWidget
			Self.focusWidget = Self.newFocusWidget
			If Self.focusWidget <> Null And Self.focusWidget.parent <> Null
				Self.focusWidget.Remove()
				
				' Will be the last in the list (drawn last)
				Self.focusWidget.parent.Add(Self.focusWidget)
			EndIf
		EndIf
	End Method
	
	' Draw
	Method Draw() 
		Self.root.DrawAll() 
		Self.popupMenuContainer.DrawAll()
		
		ResetMax2D()
		If Self.cursor <> Null
			Self.cursor.Draw()
		EndIf
	End Method
	
	' SetAlpha
	Method SetAlpha(nAlpha:Float)
		Self.alpha = nAlpha
	End Method
	
	' SetFont
	Method SetFont(font:TImageFont)
		Self.root.SetFontAll(font)
	End Method
	
	' SetCursor
	Method SetCursor(name:String)
		Self.cursor = Self.skin.GetCursor(name)
	End Method
	
	' InitClass
	Function InitClass()
		TSkin.InitClass(FS_ROOT + "data/gui/") 
		TSlider.InitClass()
		
		?Debug
			TGUI.logger = TLog.Create(StandardIOStream)
		?Not Debug
			TGUI.logger = TLog.Create(WriteFile(FS_ROOT + "logs/gui.txt"))
		?
		
		TGUI.initializedClass = True
	End Function
	
	' Create
	Function Create:TGUI()
		Local lgui:TGUI = New TGUI
		lgui.Init()
		Return lgui
	End Function
End Type
